package com.yang.provider.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @author ：YangRan
 * @date ：Created in 2021/11/21 22:50
 * @description：Redisson配置类
 * @modified By：
 * @version: 1.0
 */
@Configuration
public class RedissonConfig {

    @Value("${spring.redis.cluster.nodes}")
    private String nodes;

    @Bean
    public RedissonClient redissonClient() {
        String[] nodeArray = nodes.split(",");
        Config config = new Config();
        // 集群实现
        //config.useClusterServers().setScanInterval(2000).addNodeAddress("redis://redis121.199.164.183:6379", "redis://redis://121.199.164.183:6380").addNodeAddress("redis://redis://121.199.164.183:6381");
        config.useClusterServers().setScanInterval(2000).
                addNodeAddress("redis://" + nodeArray[5]).
                addNodeAddress("redis://" + nodeArray[4]).
                addNodeAddress("redis://" + nodeArray[3]).
                addNodeAddress("redis://" + nodeArray[2]).
                addNodeAddress("redis://" +  nodeArray[1]).
                addNodeAddress("redis://" + nodeArray[0]);


        // 单节点
        //config.useSingleServer().setAddress("redis://" + host + ":" + port);
        return Redisson.create(config);
    }
}

